package com.ubis.ibms.plugin.dialogs;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Spinner;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;

import com.ubis.ibms.plugin.common.verify.CompanyVerify;
import com.ubis.ibms.plugin.dao.CompanyManager;
import com.ubis.ibms.plugin.model.Company;

public class CompanyAddDialog extends Dialog {
	
	// 公司名称
	private Text nameText;
	// 公司编号
	private Text codeText;
	// 联系人
	private Text perText;
	// 电话
	private Text telText;
	// 固定电话
	private Text ftelText;
	// 预存款
	private Text balanceText;
	// 提醒阈值
	private Spinner remindSpinner;
	//警告
	private Label warnLabel;
	//添加
	private Button addButton;
	//清空
	private Button clearButton;
	
	private Composite container;
		
	/**
	 * Create the dialog.
	 * @param parentShell
	 */
	public CompanyAddDialog(Shell parentShell) {
		super(parentShell);
	}

	/**
	 * Create contents of the dialog.
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		container = (Composite) super.createDialogArea(parent);
		container.setLayout(new GridLayout(6, false));
		
		// 公司名称
		Label nameLabel1 = new Label(container, SWT.RIGHT);
		nameLabel1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		nameLabel1.setText("公司名称：");
		nameText = new Text(container, SWT.BORDER);
		nameText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				3, 1));

		// 公司编号
		Label idLabel = new Label(container, SWT.RIGHT);
		idLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false,
				1, 1));
		idLabel.setText("公司编号：");
		codeText = new Text(container, SWT.BORDER);
		codeText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1,
				1));

		// 联系人
		Label perLabel = new Label(container, SWT.RIGHT);
		perLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		perLabel.setText("联系人：");
		perText = new Text(container, SWT.BORDER);
		perText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));

		// 电话
		Label telLabel = new Label(container, SWT.RIGHT);
		telLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		telLabel.setText("电话：");
		telText = new Text(container, SWT.BORDER);
		telText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));

		// 固定电话
		Label ftelLabel = new Label(container, SWT.RIGHT);
		ftelLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		ftelLabel.setText("固定电话：");
		ftelText = new Text(container, SWT.BORDER);
		ftelText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));

		// 预存款
		Label balanceLabel1 = new Label(container, SWT.RIGHT);
		balanceLabel1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		balanceLabel1.setText("预存款：");
		balanceText = new Text(container, SWT.BORDER);
		balanceText.setText("0");
		balanceText.setEditable(false);
		balanceText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1));
		Label balanceLabel2 = new Label(container, SWT.LEFT);
		balanceLabel2.setText("元");

		// 提醒阈值
		Composite remindComposite = new Composite(container, SWT.NONE);
		remindComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 3, 1));
		remindComposite.setLayout(new GridLayout(3, false));
		Label remindLabel1 = new Label(remindComposite, SWT.RIGHT);
		remindLabel1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		remindLabel1.setText("余额小于");
		remindSpinner = new Spinner(remindComposite, SWT.BORDER);
		Label remindLabel2 = new Label(remindComposite, SWT.LEFT);
		remindLabel2.setText("天开销时提醒充值");
		
		warnLabel = new Label(container, SWT.NONE);
		warnLabel.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
		warnLabel.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false,
				false, 6, 1));
		
		return container;
	}

	/**
	 * Create contents of the button bar.
	 * @param parent
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		parent.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
				false, 1, 1));
		parent.setLayout(new GridLayout(2, false));
		addButton = createButton(parent, IDialogConstants.CLIENT_ID, "添加",
				false);
		addButton.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true,
				false, 1, 1));
		addButton.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				// 点击添加时
				System.out.println("addButton mouse up.");
				//添加公司信息
				Company company = new Company();
				addCompanyInfo(company);
				//company信息验证
				String message = CompanyVerify.addCompanyVerify(company);
				//如果是SUCCESS则添加，不是则显示警告信息
				if(message.equals("SUCCESS")){
					CompanyManager.save(company);//保存company
					MessageDialog.openInformation(CompanyAddDialog.this.getShell(), "充值成功",
							"登记成功！");
					close();
				}
				else{
					warnLabel.setText(message);
					CompanyAddDialog.this.container.layout();
					
				}
			}
		});
		clearButton = createButton(parent, IDialogConstants.CLIENT_ID,
				"清空", false);
		clearButton.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true,
				false, 1, 1));
		clearButton.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				// 点击清空时
				clearInput();
			}
		});
	}

	/**
	 * Return the initial size of the dialog.
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(450, 200);
	}

	@Override
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText("业主登记");
	}

	/**
	 * 清空输入
	 */
	public void clearInput(){
		nameText.setText("");
		codeText.setText("");
		perText.setText("");
		telText.setText("");
		ftelText.setText("");
		balanceText.setText("0");
		remindSpinner.setSelection(0);
	}
	
	/**
	 * 添加公司信息
	 */
	public void addCompanyInfo(Company company){
		company.setName(nameText.getText().trim());
		company.setCode(codeText.getText().trim());
		company.setPer(perText.getText().trim());
		company.setTel(telText.getText().trim());
		company.setPhone(ftelText.getText().trim());
		company.setBalance(Double.parseDouble(balanceText.getText().trim()));
		company.setRemindDay(Integer.parseInt(remindSpinner.getText()));
	}
}
